package me.qi.kancha.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.qi.kancha.dto.core.RoleRuleDTO;
import me.qi.kancha.mapper.RoleMapper;
import me.qi.kancha.dto.core.RoleDTO;
import me.qi.kancha.service.EntityRoleService;
import me.qi.kancha.service.RoleRuleService;
import me.qi.kancha.service.RoleService;
import me.qi.kancha.service.base.support.PageData;
import me.qi.kancha.support.U;
import me.qi.kancha.vo.role.RoleGrantVo;
import me.qi.kancha.vo.role.RoleInsertVo;
import me.qi.kancha.vo.role.RoleQueryVo;
import me.qi.kancha.vo.role.RoleUpdateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author kobe
* @description 针对表【role】的数据库操作Service实现
* @createDate 2022-01-06 14:51:48
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleDTO>
    implements RoleService {

    private final RoleMapper roleMapper;
    private final RoleRuleService roleRuleService;
    private final EntityRoleService entityRoleService;

    public RoleServiceImpl(RoleMapper roleMapper, RoleRuleService roleRuleService, EntityRoleService entityRoleService) {
        this.roleMapper = roleMapper;
        this.roleRuleService = roleRuleService;
        this.entityRoleService = entityRoleService;
    }

    @Override
    public PageData<RoleDTO> listPaging(Object paramObj) {
        RoleQueryVo param = (RoleQueryVo) paramObj;
        Page page;
        if (!ObjectUtils.isEmpty(param.getKey())) {
            page = lambdaQuery().eq(RoleDTO::getCode, param.getKey())
                    .or().like(RoleDTO::getName, param.getKey())
                    .or().like(RoleDTO::getScope, param.getKey())
                    .page(param.pageInstance());
        } else {
            page = lambdaQuery().page(param.pageInstance());
        }
        return new PageData<>(page);
    }

    @Override
    public RoleDTO insert(Object paramObj) {
        RoleInsertVo param = (RoleInsertVo) paramObj;
        RoleDTO dto = new RoleDTO();
        BeanUtils.copyProperties(param, dto);
        save(dto);
        return dto;
    }

    @Override
    public RoleDTO update(Object paramObj) {
        RoleUpdateVo param = (RoleUpdateVo) paramObj;
        RoleDTO dto = new RoleDTO();
        BeanUtils.copyProperties(param, dto, U.getNullPropertyNames(param));
        updateById(dto);
        return getById(param.getId());
    }

    @Override
    public boolean grant(RoleGrantVo param) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("role_code", param.getCode());
        roleRuleService.remove(wrapper);//角色权限关联删除
        if (!ObjectUtils.isEmpty(param.getRuleCodes())) {
            List<RoleRuleDTO> list = param.getRuleCodes().stream().map(code -> {
                RoleRuleDTO dto = new RoleRuleDTO();
                dto.setRoleCode(param.getCode());
                dto.setRuleCode(code);
                return dto;
            }).collect(Collectors.toList());
            return roleRuleService.saveBatch(list);
        }
        return true;
    }

    @Override
    public List<String> ruleCodes(String code) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("role_code", code);
        List<RoleRuleDTO> list = roleRuleService.list(wrapper);
        return list.stream().map(roleRuleDTO -> roleRuleDTO.getRuleCode())
                .collect(Collectors.toList());
    }

    @Override
    public void delete(String code) {
        QueryWrapper wrapper1 = new QueryWrapper();
        wrapper1.eq("code", code);
        remove(wrapper1);//根据编号删除
        QueryWrapper wrapper2 = new QueryWrapper();
        wrapper2.eq("role_code", code);
        roleRuleService.remove(wrapper2);//角色权限关联删除
        QueryWrapper wrapper3 = new QueryWrapper();
        wrapper3.eq("role_code", code);
        entityRoleService.remove(wrapper3);//实体角色关联删除
    }
}




